//
//  TravelDetailViewController.m
//  B
//
//  Created by Sunpeng on 15/11/13.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "TravelDetailViewController.h"
#import <MapKit/MapKit.h>
#import "MKAnnotation.h"
#import "UIView+Frame.h"
#import "HTTPTool.h"
#import "DetailCollectionViewCell.h"
#import "TravelCollectionReusableView.h"
#import "MapViewController.h"

@interface TravelDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,MKMapViewDelegate>

@property (nonatomic, retain)UIButton *btn;

@property (nonatomic, assign)NSInteger count;
@end

@implementation TravelDetailViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [self.tabBarController.tabBar setHidden:NO];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[[self.navigationController.navigationBar subviews]objectAtIndex:0] setAlpha:1];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
   
    UIImage *image = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    UIImage *img = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithImage:img style:UIBarButtonItemStylePlain target:self action:@selector(pop)];
    self.navigationItem.leftBarButtonItem = left;
    
    self.daysArray = [NSMutableArray array];
    
    self.detail = [[DetailView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT - 49)];
    
    self.detail.myCollectionView.delegate = self;
    self.detail.myCollectionView.dataSource = self;
    self.detail.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.detail.myCollectionView registerClass:[DetailCollectionViewCell  class] forCellWithReuseIdentifier:@"Identifier"];
    
    [self.detail.myCollectionView registerClass:[TravelCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"dayHeader"];
    [self.view addSubview:self.detail];
    
    
    [self getData];
}
- (void)setMap{
    
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.btn.frame = CGRectMake(0, 0, WIDTH, HIGHT*0.25);
    [self.btn setImage:[UIImage imageNamed:@"trip_map_expand_button"] forState:UIControlStateNormal];
    self.btn.center = self.detail.mapImage.center;
    
    [self.btn addTarget:self action:@selector(inspectMap) forControlEvents:UIControlEventTouchUpInside];
    [self.detail.myCollectionView addSubview:self.btn];
    
}

- (void)inspectMap{
    MapViewController *map = [[MapViewController alloc]init];
    map.daysArray = [NSMutableArray arrayWithArray:self.daysArray];
    map.urlStr = self.UrlStr;
    [self.navigationController pushViewController:map animated:YES];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.daysArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [[[self.daysArray objectAtIndex:section]waypoints]count];
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Identifier" forIndexPath:indexPath];
    daysModel *days = [self.daysArray objectAtIndex:indexPath.section];
    dayModel *day = [days.waypoints objectAtIndex:indexPath.row];
    [cell setData:day];
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    daysModel *days = [self.daysArray objectAtIndex:indexPath.section];
    dayModel *day = [days.waypoints objectAtIndex:indexPath.row];
    NSInteger h = [day.photo_h integerValue];
    NSInteger w = [day.photo_w integerValue];
    UILabel *tmpLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH * 0.8, 0)];
    tmpLabel.text = day.text;
    tmpLabel.numberOfLines = 0;
    [tmpLabel sizeToFit];
    
    if (w==0) {
        CGSize size = CGSizeMake(WIDTH*0.9, tmpLabel.height + HIGHT * 0.05);
        return size;
    }
    else{
        CGSize size = CGSizeMake(WIDTH*0.9, h/(w/(WIDTH* 0.9)) + tmpLabel.height + HIGHT * 0.05);
        //        图片高 文字高 底部高
        return size;
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //    NSLog(@"%@",indexPath);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableview = nil;
    
    if (kind == UICollectionElementKindSectionHeader){
        daysModel *days = [self.daysArray objectAtIndex:indexPath.section];
        TravelCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"dayHeader" forIndexPath:indexPath];
        NSString *head = [NSString stringWithFormat:@"第%@天 %@",days.day,days.date];
        headerView.titleLabel.text = head;
        reusableview = headerView;
    }
    
    return reusableview;
}

- (void)getData{
    
    
    [HTTPTool get:self.UrlStr body:nil httpResult:JSON success:^(id result) {
        //http://api.breadtrip.com//destination/place/%@/%@/photos/?gallery_mode=1&count=18&start=%ld
        
        DetailModel *detail = [[DetailModel alloc]init];
        [detail setValuesForKeysWithDictionary:result];
        detail.user_name = [[result objectForKey:@"user"]objectForKey:@"name"];
        detail.user_avatar = [[result objectForKey:@"user"]objectForKey:@"avatar_m"];
        
        NSArray *tmpArray = [result objectForKey:@"days"];
        
        for (NSDictionary *Dic in tmpArray) {
            daysModel *days = [[daysModel alloc]init];
            days.date = [Dic objectForKey:@"date"];
            days.day = [Dic objectForKey:@"day"];
            
            NSMutableArray *array = [[NSMutableArray alloc]init];
            
            for (NSDictionary *dic in [Dic objectForKey:@"waypoints"]) {
                dayModel *day = [[dayModel alloc]init];
                [day setValuesForKeysWithDictionary:dic];
                if ([[dic objectForKey:@"photo_1600"]length] != 0) {
                    day.photo_h = [[dic objectForKey:@"photo_info"]objectForKey:@"h"];
                    day.photo_w = [[dic objectForKey:@"photo_info"]objectForKey:@"w"];
                }
                if ((NSNull *)[dic objectForKey:@"location"] != [NSNull null]) {
                    [day setValuesForKeysWithDictionary:[dic objectForKey:@"location"]];
                }
                if ((NSNull *)[dic objectForKey:@"poi"] != [NSNull null]) {
                    [day setValuesForKeysWithDictionary:[dic objectForKey:@"poi"]];
                }
                
                [array addObject:day];
                
            }
            days.waypoints = [NSMutableArray arrayWithArray:array];
            [self.daysArray addObject:days];
          
        }
        
        [self.detail.myCollectionView reloadData];
        
        [self.detail setDetailViewData:detail];
        [self setMap];
        
        
        
        
       
        
        
    } failure:^(NSError *error) {
        NSLog(@"dddddd");
    }];
    
}


- (void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
